package com.byb.server.service.impl;

import com.byb.server.service.EnsureIndex2Service;
import com.byb.server.domain.EnsureIndex2;
import com.byb.server.repository.EnsureIndex2Repository;
import com.byb.server.repository.search.EnsureIndex2SearchRepository;
import com.byb.server.service.dto.EnsureIndex2DTO;
import com.byb.server.service.mapper.EnsureIndex2Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing EnsureIndex2.
 */
@Service
@Transactional
public class EnsureIndex2ServiceImpl implements EnsureIndex2Service{

    private final Logger log = LoggerFactory.getLogger(EnsureIndex2ServiceImpl.class);

    private final EnsureIndex2Repository ensureIndex2Repository;

    private final EnsureIndex2Mapper ensureIndex2Mapper;

    private final EnsureIndex2SearchRepository ensureIndex2SearchRepository;
    public EnsureIndex2ServiceImpl(EnsureIndex2Repository ensureIndex2Repository, EnsureIndex2Mapper ensureIndex2Mapper, EnsureIndex2SearchRepository ensureIndex2SearchRepository) {
        this.ensureIndex2Repository = ensureIndex2Repository;
        this.ensureIndex2Mapper = ensureIndex2Mapper;
        this.ensureIndex2SearchRepository = ensureIndex2SearchRepository;
    }

    /**
     * Save a ensureIndex2.
     *
     * @param ensureIndex2DTO the entity to save
     * @return the persisted entity
     */
    @Override
    public EnsureIndex2DTO save(EnsureIndex2DTO ensureIndex2DTO) {
        log.debug("Request to save EnsureIndex2 : {}", ensureIndex2DTO);
        EnsureIndex2 ensureIndex2 = ensureIndex2Mapper.toEntity(ensureIndex2DTO);
        ensureIndex2 = ensureIndex2Repository.save(ensureIndex2);
        EnsureIndex2DTO result = ensureIndex2Mapper.toDto(ensureIndex2);
       // ensureIndex2SearchRepository.save(ensureIndex2);
        return result;
    }

    /**
     *  Get all the ensureIndex2S.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<EnsureIndex2DTO> findAll(Pageable pageable) {
        log.debug("Request to get all EnsureIndex2S");
        return ensureIndex2Repository.findAll(pageable)
            .map(ensureIndex2Mapper::toDto);
    }

    /**
     *  Get one ensureIndex2 by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public EnsureIndex2DTO findOne(Long id) {
        log.debug("Request to get EnsureIndex2 : {}", id);
        EnsureIndex2 ensureIndex2 = ensureIndex2Repository.findOne(id);
        return ensureIndex2Mapper.toDto(ensureIndex2);
    }

    /**
     *  Delete the  ensureIndex2 by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete EnsureIndex2 : {}", id);
        ensureIndex2Repository.delete(id);
        //ensureIndex2SearchRepository.delete(id);
    }

    /**
     * Search for the ensureIndex2 corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<EnsureIndex2DTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of EnsureIndex2S for query {}", query);
        Page<EnsureIndex2> result = ensureIndex2SearchRepository.search(queryStringQuery(query), pageable);
        return result.map(ensureIndex2Mapper::toDto);
    }
}
